
// <copyright file="AssemblyBuilder.cs" company="Subhadra Software Solutions">
//   
// </copyright>
// <summary>
//   The assembly builder.
// </summary>

namespace SubhadraSolutions.Sharp.Utils.Reflection
{
    using System.CodeDom.Compiler;
    using System.Collections.Generic;

    /// <summary>
    /// The assembly builder.
    /// </summary>
    public class AssemblyBuilder
    {
        #region Public Methods and Operators

        /// <summary>
        /// The build assembly.
        /// </summary>
        /// <param name="code">
        /// The code.
        /// </param>
        /// <param name="programmingLanguage">
        /// The programming language.
        /// </param>
        /// <param name="assembliesToInclude">
        /// The assemblies to include.
        /// </param>
        /// <param name="generateInMemory">
        /// The generate in memory.
        /// </param>
        /// <returns>
        /// The <see cref="CompilerResults"/>.
        /// </returns>
        public static CompilerResults BuildAssembly(string code, string programmingLanguage, List<string> assembliesToInclude, bool generateInMemory)
        {
            CodeDomProvider domProvider = CodeDomProvider.CreateProvider(programmingLanguage);
            var parameters = new CompilerParameters();
            foreach (string assembly in assembliesToInclude)
            {
                parameters.ReferencedAssemblies.Add(assembly);
            }

            parameters.GenerateInMemory = generateInMemory;
            return domProvider.CompileAssemblyFromSource(parameters, code);
        }

        /// <summary>
        /// The build assembly.
        /// </summary>
        /// <param name="code">
        /// The code.
        /// </param>
        /// <param name="programmingLanguage">
        /// The programming language.
        /// </param>
        /// <param name="parameters">
        /// The parameters.
        /// </param>
        /// <returns>
        /// The <see cref="CompilerResults"/>.
        /// </returns>
        public static CompilerResults BuildAssembly(string code, string programmingLanguage, CompilerParameters parameters)
        {
            CodeDomProvider domProvider = CodeDomProvider.CreateProvider(programmingLanguage);
            return domProvider.CompileAssemblyFromSource(parameters, code);
        }

        #endregion Public Methods and Operators
    }
}
